package com.fc.test.controller.gen;

import com.alibaba.fastjson.JSONObject;
import com.fc.test.common.base.BaseController;
import com.fc.test.common.domain.AjaxResult;
import com.fc.test.model.auto.EntInfo;
import com.fc.test.model.auto.EntJob;
import com.fc.test.model.auto.EntUser;
import com.fc.test.model.auto.StuCv;
import com.fc.test.model.custom.TableSplitResult;
import com.fc.test.model.custom.Tablepar;
import com.fc.test.model.custom.TitleVo;
import com.fc.test.service.EntInfoService;
import com.fc.test.service.EntUserService;
import com.fc.test.service.StuCvService;
import com.fc.test.service.StuPostRecordService;
import com.github.pagehelper.PageInfo;
import com.google.gson.JsonObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "企业")
@Controller
@RequestMapping("/EntBaseController")
public class EntBaseController extends BaseController{
	
	private String prefix = "gen/entUser";
	@Autowired
	private EntUserService entUserService;
	@Autowired
	private EntInfoService entInfoService;
	@Autowired
	private StuPostRecordService stuPostRecordService;
	@Autowired
	private StuCvService stuCvService;
	
	/**
	 * 分页跳转
	 */
	@ApiOperation(value = "分页跳转", notes = "分页跳转")
	@GetMapping("/view")
    public String view(ModelMap model)
    {	
		String str="企业用户";
		setTitle(model, new TitleVo("列表", str+"管理", true,"欢迎进入"+str+"页面", true, false));
        return prefix + "/list";
    }
	
	/**
	 * 分页查询
	 */
	//@Log(title = "企业集合查询", action = "111")
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@PostMapping("/list")
	@ResponseBody
	public Object list(@RequestBody JSONObject request){
		Tablepar tablepar = new Tablepar();
		PageInfo<EntUser> page1=entUserService.list(tablepar,request.getJSONObject("EntUser").toJavaObject(EntUser.class)) ;
		PageInfo<EntInfo> page2=entInfoService.list(tablepar,request.getJSONObject("EntInfo").toJavaObject(EntInfo.class)) ;
		TableSplitResult<EntUser> result1=new TableSplitResult<EntUser>(page1.getPageNum(), page1.getTotal(), page1.getList());
		TableSplitResult<EntInfo> result2=new TableSplitResult<EntInfo>(page2.getPageNum(), page2.getTotal(), page2.getList());
		JSONObject result = new JSONObject();
		result.put("EntUser", result1);
		result.put("EntInfo", result2);

		return  result;
	}
	
	/**
     * 新增跳转
     */
    @ApiOperation(value = "新增跳转", notes = "新增跳转")
    @GetMapping("/add")
    public String add(ModelMap modelMap)
    {
        return prefix + "/add";
    }
	
	/**
     * 新增
     */
	//@Log(title = "企业新增", action = "111")
   	@ApiOperation(value = "新增", notes = "新增")
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult add(@RequestBody JSONObject request){

		int c=entInfoService.insertSelective(request.getJSONObject("EntInfo").toJavaObject(EntInfo.class));
		if(c>0){
			EntUser user=request.getJSONObject("EntUser").toJavaObject(EntUser.class);
			user.setEid(c);
			int b=entUserService.insertSelective(user);
			if(b>0){
				return success();
			}
		}
		return error("Half-succeed | Please Check Database And Rollback!");


	}
	
	/**
	 * 删除
	 * @param ids
	 * @return
	 */
	//@Log(title = "企业删除", action = "111")
	@ApiOperation(value = "删除", notes = "删除")
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids){
		int b=entUserService.deleteByPrimaryKey(ids);
		int c=entInfoService.deleteByPrimaryKey(ids);
		if(b>0&&c>0){
			return success();
		}else if(!(b>0)&&!(c>0)){
			return error();
		}else {
			return error("Half-succeed | Please Check Database And Rollback!");
		}
	}

	public int checkNameUnique(){
		return 0;
	}
	
	
	/**
	 * 修改跳转
	 * @param id
	 * @param mmap
	 * @return
	 */
	@ApiOperation(value = "修改跳转", notes = "修改跳转")
	@GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        mmap.put("EntUser", entUserService.selectByPrimaryKey(id));
		mmap.put("EntInfo", entInfoService.selectByPrimaryKey(id));

		return prefix + "/edit";
    }
	
	/**
     * 修改保存
     */
    //@Log(title = "企业用户修改", action = "111")
    @ApiOperation(value = "修改保存", notes = "修改保存")
//    @RequiresPermissions("gen:entUser:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody JSONObject request)
    {
        if(toAjax(entUserService.updateByPrimaryKeySelective(request.getJSONObject("EntUser").toJavaObject(EntUser.class))).equals(success())
				&&toAjax(entInfoService.updateByPrimaryKeySelective(request.getJSONObject("EntInfo").toJavaObject(EntInfo.class))).equals(success())){
        	return success();
		}
		else {
			return error();
		}
    }

	/**
	 * 修改保存密码
	 */
	@ApiOperation(value = "修改保存密码", notes = "修改保存密码")
	@PostMapping("/editPw")
	@ResponseBody
	public AjaxResult editPw(@RequestBody JSONObject request)
	{
		EntUser entUser = entUserService.findByName(request.getString("username"));
		if(entUser != null){
			if(entUser.getUpassword().equals(request.getString("oldPassword"))){
				return toAjax(entUserService.updatePasswordByPrimaryKeySelective(String.valueOf(entUser.getUid()), request.getString("newPassword")));
			}
			else {
				return error("密码错误");
			}
		}
		return error("当前用户名未注册");
	}

    
    /**
   	 * 根据主键查询
   	 * 
   	 * @param id
   	 * @param
   	 * @return
   	 */
   	@ApiOperation(value = "根据id查询唯一", notes = "根据id查询唯一")
   	@RequestMapping("/get/{id}")
	@ResponseBody
	public Object edit(@PathVariable("id") String id) {
   		JSONObject result = new JSONObject();
   		result.put("EntUser", entUserService.selectByPrimaryKey(id));
   		result.put("EntInfo", entInfoService.selectByPrimaryKey(id));

   		return result;
   	}

	/**
	 * 查看投递的简历
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "查看投递简历情况", notes = "查看投递简历情况")
	@RequestMapping("/listCv")
	public Object listCv(@PathVariable("id") String id) {
		return stuPostRecordService.selectByEid(id);
	}

	/**
	 * 模糊查询
	 */
	//@Log(title = "学生简历信息集合查询", action = "111")
	@ApiOperation(value = "模糊查询", notes = "模糊查询")
	@RequestMapping("/search")
	@ResponseBody
	public Object search(Tablepar tablepar, String keyword){
		List<StuCv> result = stuCvService.search(keyword);
		return result;

	}

	/**
	 * 修改简历状态
	 */
	//@Log(title = "学生简历状态修改", action = "111")
	@ApiOperation(value = "修改简历状态", notes = "修改简历状态")
	@RequestMapping("/updateCv")
	@ResponseBody
	public AjaxResult updateCv(@PathVariable("id") String id, @PathVariable("status") Integer status){
		return toAjax(stuPostRecordService.updateStatusBySid(id, status));
	}

	/**
	 * 企业登录
	 */
	//@Log(title = "企业登录", action = "111")
	@ApiOperation(value = "企业登录", notes = "企业登录")
	@PostMapping("/login")
	@ResponseBody
	public AjaxResult login(@PathVariable("name") String name, @PathVariable("upassword") String upassword)
	{
		if(entUserService.findByName(name).getUpassword().equals(upassword)){
			return success();
		}
		else{
			return error();
		}
	}



}
